package com.dl698.parse;

import com.dl698.axdr.*;
import com.dl698.bean.FrameData;
import com.dl698.util.DataUtils;
import com.dl698.tools.ResultCode;
import com.dl698.asn1.axdr.AxdrEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ParseSetResponse类
 * 实现设置响应解析功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class ParseSetResponse {

    public static String parseSetNormalResponse(SetResponseNormal setNormalResponse) {
        Map<String, Object> setResponseNormal = new HashMap<>();
        FrameData frameData = new FrameData();

        setNormalResponse.getResult().getDar().encodeAndSave(1000);
        setResponseNormal.put("dar", DataUtils.bytesToHexString(
                setNormalResponse.getResult().getDar().getDataCode(),
                0,
                setNormalResponse.getResult().getDar().getDataCode().length));

        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(setResponseNormal);
        return frameData.toJsonString();
    }

    public static String parseSetNormalResponseList(SetResponseNormalList setResponseNormalList) {
        Map<String, Object> setResponseList = new HashMap<>();
        FrameData frameData = new FrameData();
        ArrayList<Object> seqOfDar = new ArrayList<>();

        for (int i = 0; i < setResponseNormalList.getResults().size(); i++) {
            Map<String, Object> dar = new HashMap<>();
            A_ResultNormal setoadDar = setResponseNormalList.getResults().get(i);
            setoadDar.getResult().getDar().encodeAndSave(1000);
            dar.put("dar", DataUtils.bytesToHexString(
                    setoadDar.getResult().getDar().getDataCode(),
                    0,
                    setoadDar.getResult().getDar().getDataCode().length));
            seqOfDar.add(dar);
        }

        setResponseList.put("seq_of_dar", seqOfDar);
        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(setResponseList);
        return frameData.toJsonString();
    }

    public static String parseSetThenGetResponse(SetThenGetResponseNormalList setThenGetResponse) {
        Map<String, Object> setThenGetList = new HashMap<>();
        FrameData frameData = new FrameData();
        ArrayList<Object> resultArray = new ArrayList<>();

        for (int i = 0; i < setThenGetResponse.getReads().size(); i++) {
            Map<String, Object> result = new HashMap<>();
            Map<String, Object> getResult = new HashMap<>();

            A_ResultNormal aResultNormal = setThenGetResponse.getReads().get(i);
            // 修复：正确访问A_ResultNormal的属性
            Get_Result get_result = aResultNormal.getResult();
            if (get_result != null) {
                get_result.getDar().encodeAndSave(1000);
                result.put("seq_dar", DataUtils.bytesToHexString(
                        get_result.getDar().getDataCode(),
                        0,
                        get_result.getDar().getDataCode().length));

                switch (get_result.getChoiceIndex().getValue()) {
                    case 0: { // DAR
                        Map<String, Object> dar = new HashMap<>();
                        AxdrEnum darEnum = get_result.getDar();
                        if (darEnum != null) {
                            darEnum.encodeAndSave(1000);
                            dar.put("dar", DataUtils.bytesToHexString(
                                    darEnum.getDataCode(),
                                    0,
                                    darEnum.getDataCode().length));
                        }
                        getResult.put("dar", dar);
                        break;
                    }
                    case 1: { // DATA
                        Data resultData = get_result.getData();
                        if (resultData != null) {
                            getResult.put("data", ParseTools.parseData(resultData));
                        }
                        break;
                    }
                }
            }

            result.put("get_result", getResult);
            resultArray.add(result);
        }

        frameData.setCode(ResultCode.SUCCESS);
        setThenGetList.put("seq_result", resultArray);
        frameData.setData(setThenGetList);
        return frameData.toJsonString();
    }
}